package modelo.persistir;

import java.util.ArrayList;

import modelo.mapa.Ubicacion;
import modelo.servicios.LineaDeTension;
import modelo.servicios.RedElectrica;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class RedElectricaPersistir {
	
	public static JsonArray persistirArray(ArrayList<RedElectrica> elems)
	{
		Gson gson = new Gson();
		JsonObject jObject=new JsonObject();
		JsonArray jArray=new JsonArray();	
		 for(RedElectrica red:elems){
			jArray.add(RedElectricaPersistir.persistir(red.obtenerDatosPersistibles()));
		 }
		  return jArray;
	}

	public static JsonObject persistir(ArrayList elems)
	{
		Gson gson = new Gson();
		JsonObject jObject=new JsonObject();
		JsonArray jArray=new JsonArray();		
		ArrayList<LineaDeTension>elementos=(ArrayList<LineaDeTension>) elems.get(0);
		for(LineaDeTension linea:elementos){
			jArray.add(LineaDeTensionPersistir.persistir(linea.obtenerDatosPersistibles()));			
		}
		jObject.add("Lineas De Tension",jArray );
		return jObject;
	}

	public static ArrayList cargar(String comp) 
	{
		ArrayList lista=new ArrayList();
		JsonParser parser = new JsonParser();
		JsonObject jObject=parser.parse(comp).getAsJsonObject();
		JsonArray jArray=parser.parse(jObject.get("Lineas De Tension").toString()).getAsJsonArray();
		for(int i=0;i<jArray.size();i++){
			LineaDeTension lin=new LineaDeTension();
			lin.poblar(LineaDeTensionPersistir.cargar(jArray.get(i).toString()));
			lista.add(lin);			
		}
		return lista;
	}

	public static ArrayList cargarArray(String comp) 
	{
		ArrayList lista=new ArrayList();
		JsonParser parser = new JsonParser();
		JsonArray jArray=parser.parse(comp).getAsJsonArray();
		for(int i=0;i<jArray.size();i++){
			RedElectrica red=new RedElectrica(null);
			red.poblar(RedElectricaPersistir.cargar(jArray.get(i).toString()));
			lista.add(red);
		}	
		return lista;
	}
}
